<?php
class controller_base
{
    public $_view;
    public $_mod;
    public $_act;
    public $_page_num;
    
    protected $_succ = 'succ';
    protected $_fail = 'fail';
    
    public function __construct()
    {
        $this->_view = new lib_view();
        $this->__init();
    }
    /**
     * 
     * 初始化函数
     */
    private function __init()
    {
        $this->_mod = isset( $_GET['mod'] ) ? $_GET['mod'] : 'index';
        $this->_act = isset( $_GET['act'] ) ? $_GET['act'] : 'index';
        
        //@author mingyu@leju.sina.com.cn 增加判断是否是系统管理员
        $this->_is_sys_admin = module_base::is_admin();
        $this->_view->assign( 'is_sys_admin' , $this->_is_sys_admin );
        
        $this->_view->assign( 'act', $this->_act );
        $this->_view->assign( 'mod', $this->_mod );
       
        $this->_page_num = 15;
        
        $this->_view->assign( 'scripts_path', '../resources/scripts' );
        $this->_view->assign( 'css_path', '../resources/css' );
        $this->_view->assign( 'images_path', '../resources/images' );
        
    }
    
    
    
    //系统设置
    public function __isset($name)
    {
        return isset($this->$name);
    }
    
    public function __call($name, $arguments)
    {
        if (!$this->__isset($name))
        {
            throw new lib_base_exception('此操作不存在' . $name);
            exit();
        }
    }
    /**
     * 
     * 后台列表页
     */
    public function _base_list( $condition, $data_obj, $order = '' )
    {
        foreach ( $_POST  as $key => $val )
        {
            $_GET[$key] = $val;
        }
        //获取记录总数
        $count = $data_obj->fetch_count_by_condition( $condition );
        //分页信息
        $limit = '';
        if( $count > 0 )
		{
			$pager = new module_pager2( $count, $this->_page_num );
			$this->_view->assign( 'pager', $pager->gethtml() );
            $this->_view->assign( 'pager_url', $pager->geturl() );
            $limit = $pager->get_limit();
		}

        //获取列表数据
        $item_list = $data_obj->fetch_all_by_condition_order_limit( $condition, $order, $limit );
        $this->_view->assign( 'get', $_GET );
        $this->_view->assign( 'item_list', $item_list );
        return $item_list;
    }
    /**
     * 消息提示页
     */
    public function _alert($msg, $go_url = '')
	{
		$this->_view->assign('msg', $msg);
		$this->_view->assign('go_url', $go_url);
		$this->_view->display('msg.html');
		exit();
	}
    
    /**
     * 将要输出的数据格式成需要的形式返回
     * @param boolean $is_succ
     * @param mixed   $info
     * @return array
     */
    protected function _formatreturndata($is_succ, $info = null)
    {
        $res = array();
        if($is_succ) {
            $res['result'] = $this->_succ;
            $res['info'] = $info;
        } else {
            $res['result'] = $this->_fail;
            $res['reason'] = $info;
        }
        return $res;
    }
}